﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server.Dao;
using System.Reflection;

namespace MaxStudio.Concrete.WebSite.Controllers
{
    public class JsonController : Controller
    {
        //
        // GET: /Json/

        public ActionResult FlexigridList()
        {
            int page = int.Parse(Request.Form["page"]);
            int rp = int.Parse(Request.Form["rp"]);
            string qtype = Request.Form["qtype"].ToString();
            string query = Request.Form["query"].ToString();
            string sortname = Request.Form["sortname"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();

            FunctionesService serviceObj = ServiceFactory.getFunctionesService();

            OperationResult<List<tb_FunctionItem>> result = serviceObj.getAllFunctionList();

            List<tb_FunctionItem> functionItemList = result.obj.ToList();
            FlexigridObject flexigridObject = new FlexigridObject();
            flexigridObject.page = page;
            flexigridObject.total = result.obj.Count();
            foreach (tb_FunctionItem functionItem in functionItemList)
            {
                FlexigridRow cell = new FlexigridRow()
                {
                    id = functionItem.Id.ToString(),
                    cell = GetPropertyList(functionItem)
                };
                flexigridObject.rows.Add(cell);
            }

            return View("FlexigridObject", flexigridObject);
        }

        private List<string> GetPropertyList(object obj)
        {
            List<string> propertyList = new List<string>();

            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                object o = property.GetValue(obj, null);
                propertyList.Add(o == null ? "" : o.ToString());
            }

            return propertyList;
        }

    }
}
